<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreRequest;
use Illuminate\Http\Request;
use Modules\Mall\Entities\Mall;
use Auth;
use Modules\Mall\Entities\Cate;
use Modules\Mall\Entities\MallPic;
class StoreController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function __construct () {
    	$this->middleware('auth');
	}

	public function index()
    {
    	$data=Mall::where('owner',Auth::user ()->id)->get();
        return view('store.index',compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
		$cates=Cate::get();

		return view('store.create',compact('cates'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreRequest $request,Mall $mall)
    {
		$mall->fill($request->except (['file','src']));
		$mall->save();
		$pics=$request->only (['src']);
		$pics=json_decode ($pics['src'],true);
		foreach($pics as $pic){
			MallPic::create([
				'mall_id'=>$mall['id'],
				'src'=>$pic['url']
			]);
		}
		return redirect('/store')->with('success', '保存成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
    	$mall=Mall::find($id);
		if($mall['owner']!=Auth()->user ()->id){
			return redirect ()->back();
		}
		$cates=Cate::get();
		$pics=MallPic::where('mall_id',$mall['id'])->pluck('src');
		$a=[];
		foreach($pics as $k=>$pic){
			$a[$k]['name']=1;
			$a[$k]['url']=$pic;
		}
		$pics=json_encode ($a,JSON_UNESCAPED_SLASHES);
		return view('store.edit', compact('mall','cates','pics'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(StoreRequest $request, $id)
    {
    	$mall=Mall::find($id);
		if($mall['owner']!=Auth()->user ()->id){
			return redirect ()->back();
		}
		$mall->update($request->except (['file','src']));
		$pics=$request->only (['src']);
		$pics=json_decode ($pics['src'],true);
		MallPic::where('mall_id',$mall['id'])->delete();
		foreach($pics as $pic){
			MallPic::create([
				'mall_id'=>$mall['id'],
				'src'=>$pic['url']
			]);
		}
		return redirect('/store')->with('success','更新成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
    	$mall=Mall::find($id);
		if($mall['owner']!=Auth()->user ()->id){
			session()->flash('danger','年轻人，你的思想很危险');
			return redirect ()->back();
		}
		$mall->delete();
		MallPic::where('mall_id',$mall['id'])->delete();
		return redirect('mall/mall')->with('success','删除成功');
    }
}
